home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume7 / determcap < prev    next >
Encoding:
Internet Message Format  |  1987-01-18  |  7.0 KB

  1. Subject:  v07i096:  Decomposing termcaps
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: Arnold D. Robbins <emoryu1!arnold>
  6. Mod.sources: Volume 7, Issue 96
  7. Archive-name: determcap
  8.  
  9. Here is a program I wrote while converting from 4.2 to 4.3. I have always
  10. wanted something like it. I hope that the rest of the net may find it
  11. useful.  There is no makefile as it is only a single C source file.
  12. I did write a man page though.
  13.  
  14. Arnold Robbins
  15. Emory University Computing Center
  16.  
  17. [  I wrote a Makefile, and added the quick hack of a mkdir() subroutine
  18.    for them that's need it.  --r$  ]
  19. #! /bin/sh
  20. # This is a shell archive.  Remove anything before this line,
  21. # then unpack it by saving it in a file and typing "sh file".
  22. # If all goes well, you will see the message "End of shell archive."
  23. # Contents:  Makefile determcap.8 determcap.c
  24. PATH=/bin:/usr/bin:/usr/ucb; export PATH
  25. echo shar: extracting "'Makefile'" '(263 characters)'
  26. if test -f 'Makefile' ; then 
  27.   echo shar: will not over-write existing file "'Makefile'"
  28. else
  29. sed 's/^X//' >Makefile <<'@//E*O*F Makefile//'
  30. X# If you don't have a mkdir(2) or a mkdir(3), enable this next line.
  31. X#DIR    = -DNEED_MKDIR_SUB
  32. XCFLAGS    = -O $(DIR)
  33. X
  34. Xdetermcap:        determcap
  35. X    $(CC) $(CFLAGS) -o determcap determcap.c
  36. X
  37. Xinstall:        determcap
  38. X    @echo Copy determcap and determcap.8 to appropriate directories
  39. @//E*O*F Makefile//
  40. if test 263 -ne "`wc -c <'Makefile'`"; then
  41.     echo shar: error transmitting "'Makefile'" '(should have been 263 characters)'
  42. fi
  43. fi # end of overwriting check
  44. echo shar: extracting "'determcap.8'" '(1491 characters)'
  45. if test -f 'determcap.8' ; then 
  46.   echo shar: will not over-write existing file "'determcap.8'"
  47. else
  48. sed 's/^X//' >determcap.8 <<'@//E*O*F determcap.8//'
  49. X.TH DETERMCAP 8 local
  50. X.SH NAME
  51. Xdetermcap \- split a termcap database into directories and files
  52. X.SH SYNOPSIS
  53. X.B determcap
  54. X[
  55. X.B verbose
  56. X] < termcap_file
  57. X.SH DESCRIPTION
  58. X.I Determcap
  59. Xis a very simple C program that reads a
  60. X.I termcap (5)
  61. Xdatabase on its standard input.
  62. XIn the directory where it is run
  63. X.I determcap
  64. Xwill produce a new directory for each terminal type entry in the termcap
  65. Xfile.
  66. XInside each such directory, there will be a file with the name of each
  67. Xcapability, and the termcap field for that capability will be in the file.
  68. XFor each comment block, there will be a file named
  69. X.BI comment. N
  70. Xwhere
  71. X.I N
  72. Xrepresents the fact that this was the 
  73. X.IR N 'th
  74. Xcomment block in the input.
  75. XThese files should be removed.
  76. X.PP
  77. X.I Determcap
  78. Xfinds its use in comparing different termcap databases.
  79. XTwo databases can be decomposed in two different directories, and then
  80. X.IR diff (1)
  81. Xcan be used via the
  82. X.B \-r
  83. X(recursive) option to compare the two databases.
  84. X.PP
  85. XIf given an argument,
  86. X.I determcap
  87. Xwill print out what it is parsing as it goes.
  88. X.PP
  89. XYour termcap file must not have any problems in it.
  90. XAll continuation lines must end in a \e.
  91. XThere can be no duplicate terminal entries (i.e. entries with the same name).
  92. X.SH SEE ALSO
  93. X.IR termcap (5)
  94. X.SH DIAGNOSTICS
  95. XSelf explanatory.
  96. X.SH BUGS
  97. X.PP
  98. XQuits at the first sign of trouble.
  99. X.PP
  100. XShould not bother to save the comments.
  101. X.PP
  102. XCan be very disk-space intensive.
  103. X.PP
  104. XNot exactly blindingly fast.
  105. X.SH AUTHOR
  106. XArnold Robbins
  107. X.br
  108. Xarnold@emory.edu
  109. @//E*O*F determcap.8//
  110. if test 1491 -ne "`wc -c <'determcap.8'`"; then
  111.     echo shar: error transmitting "'determcap.8'" '(should have been 1491 characters)'
  112. fi
  113. fi # end of overwriting check
  114. echo shar: extracting "'determcap.c'" '(3025 characters)'
  115. if test -f 'determcap.c' ; then 
  116.   echo shar: will not over-write existing file "'determcap.c'"
  117. else
  118. sed 's/^X//' >determcap.c <<'@//E*O*F determcap.c//'
  119. X#include <stdio.h>
  120. X#include <ctype.h>
  121. X
  122. Xchar tbuf[BUFSIZ * 16];        /* really big */
  123. Xchar *input ();
  124. XFILE *fp;
  125. Xint verbose = 0;
  126. X
  127. Xmain (argc, argv)
  128. Xint argc;
  129. Xchar **argv;
  130. X{
  131. X    verbose = (argc > 1);
  132. X
  133. X    while (input (tbuf) != NULL)
  134. X    {
  135. X        if (tbuf[0] == '#')
  136. X            comment ();
  137. X        else
  138. X            entry ();
  139. X    }
  140. X}
  141. X
  142. Xint usebuf = 0;
  143. X
  144. Xchar *input (bp)
  145. Xchar *bp;
  146. X{
  147. X    if (! usebuf)
  148. X        return (gets (bp));
  149. X    else
  150. X    {
  151. X        usebuf = 0;
  152. X        return (tbuf);
  153. X    }
  154. X}
  155. X
  156. Xcomment ()
  157. X{
  158. X    static int com_no = 1;
  159. X    char name[20];
  160. X
  161. X    sprintf (name, "comment.%d", com_no++);
  162. X    if ((fp = fopen (name, "w")) == NULL)
  163. X    {
  164. X        fflush (stdout);
  165. X        fprintf (stderr, "%s: could not open\n", name);
  166. X        exit (1);
  167. X    }
  168. X
  169. X    do
  170. X    {
  171. X        fprintf (fp, "%s\n", tbuf);
  172. X        if (input (tbuf) == NULL)
  173. X        {
  174. X            fclose (fp);
  175. X            exit (0);
  176. X        }
  177. X    } while (tbuf[0] == '#');
  178. X
  179. X    /* at this point, a non-comment is in the buffer */
  180. X    usebuf = 1;
  181. X    fclose (fp);
  182. X}
  183. X
  184. Xchar *getname ();
  185. Xchar *getentry ();
  186. X
  187. Xentry ()
  188. X{
  189. X    int end = strlen (tbuf) - 1;
  190. X    char *name, *fullname, *cp, *state;
  191. X    char *index ();
  192. X
  193. X    /* first, get the entire entry */
  194. X    while (tbuf[end] == '\\')
  195. X    {
  196. X        if (input (& tbuf[end]) == NULL)
  197. X        {
  198. X            fflush (stdout);
  199. X            fprintf (stderr, "stdin ended with a '\\\\'\n");
  200. X            exit (1);
  201. X        }
  202. X        end = strlen (tbuf) - 1;
  203. X    }
  204. X    /* now pull it apart */
  205. X
  206. X    state = tbuf;
  207. X    fullname = getentry (& state);
  208. X    name = getname (fullname);
  209. X
  210. X    if (verbose)
  211. X        printf ("%s\n", name);
  212. X
  213. X    dodir (name);
  214. X
  215. X    if ((fp = fopen (name, "w")) == NULL)
  216. X    {
  217. X        fflush (stdout);
  218. X        fprintf (stderr, "%s: could not open\n", name);
  219. X        exit (1);
  220. X    }
  221. X    fprintf (fp, "%s\n", fullname);
  222. X    fclose (fp);
  223. X
  224. X    while (cp = getentry (& state))
  225. X        doentry (cp);
  226. X
  227. X    if (chdir ("..") < 0)
  228. X    {
  229. X        perror ("chdir(\"..\")");
  230. X        exit (1);
  231. X    }
  232. X}
  233. X
  234. Xchar *getname (cp)
  235. Xregister char *cp;
  236. X{
  237. X    static char shortname[100];
  238. X    register int i = 0;
  239. X
  240. X    while (*cp != '|')
  241. X        cp++;
  242. X
  243. X    for (cp++; *cp != '|' && *cp != ':'; cp++)
  244. X        shortname[i++] = *cp;
  245. X    shortname[i] = '\0';
  246. X
  247. X    return (shortname);
  248. X}
  249. X
  250. Xchar *getentry (state)
  251. Xregister char **state;
  252. X{
  253. X    char *cp;
  254. X
  255. X    if (!state || ! *state || ! **state)
  256. X        return (NULL);
  257. X
  258. X    while (**state == '\t' || **state == ':')
  259. X        (*state)++;
  260. X
  261. X    cp = *state;
  262. X
  263. X    while (**state && **state != ':')
  264. X        (*state)++;
  265. X
  266. X    **state = '\0';
  267. X    (*state)++;
  268. X
  269. X    if (verbose)
  270. X        printf ("\t'%s'\n", cp);
  271. X
  272. X    return (cp);
  273. X}
  274. X
  275. Xdodir (dir)
  276. Xchar *dir;
  277. X{
  278. X    char buf[100];
  279. X
  280. X    if (mkdir (dir, 0755) < 0)
  281. X    {
  282. X        sprintf (buf, "mkdir (\"%s\")", dir);
  283. X        perror (buf);
  284. X        exit (1);
  285. X    }
  286. X    if (chdir (dir) < 0)
  287. X    {
  288. X        sprintf (buf, "chdir (\"%s\")", dir);
  289. X        perror (buf);
  290. X        exit (1);
  291. X    }
  292. X}
  293. X
  294. Xdoentry (cp)
  295. Xchar *cp;
  296. X{
  297. X    char name[3];
  298. X
  299. X    name[0] = cp[0];
  300. X    name[1] = cp[1];
  301. X    name[2] = '\0';
  302. X
  303. X    if ((fp = fopen (name, "w")) == NULL)
  304. X    {
  305. X        fflush (stdout);
  306. X        fprintf (stderr, "%s: could not open\n", name);
  307. X        exit (1);
  308. X    }
  309. X
  310. X    fprintf (fp, "%s\n", cp);
  311. X    fclose (fp);
  312. X}
  313. X
  314. X#ifdef    NEED_MKDIR_SUB
  315. Xint
  316. Xmkdir(p, u)
  317. X    char    *p;
  318. X    int         u;
  319. X{
  320. X    char     buff[BUFSIZ];
  321. X
  322. X    /* By playing with UMASK you can skip the chmod, but so it goes. */
  323. X    (void)sprintf(buff, "mkdir %s && chmod %o %s", p, u, p);
  324. X    return(system(buff) ? -1 : 0);
  325. X}
  326. X#endif    /* NEED_MKDIR_SUB */
  327. @//E*O*F determcap.c//
  328. if test 3025 -ne "`wc -c <'determcap.c'`"; then
  329.     echo shar: error transmitting "'determcap.c'" '(should have been 3025 characters)'
  330. fi
  331. fi # end of overwriting check
  332. echo shar: "End of shell archive."
  333. exit 0
  334.